package com.reactim.user.controller;

import com.reactim.user.dto.Result;
import com.reactim.user.dto.UserResponse;
import com.reactim.user.service.FriendService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 好友管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/friends")
@RequiredArgsConstructor
public class FriendController {

    private final FriendService friendService;

    /**
     * 获取好友列表
     */
    @GetMapping
    public Mono<Result<FriendListResponse>> getFriendList(@RequestParam(required = false) String username) {
        final String finalUsername = username != null ? username : "defaultUser";
        return friendService.getFriendList(finalUsername)
                .map(friends -> {
                    FriendListResponse response = new FriendListResponse();
                    response.setFriends(friends);
                    response.setTotal(friends.size());
                    return Result.success(response);
                })
                .doOnSuccess(result -> log.debug("获取好友列表: {}", finalUsername));
    }

    /**
     * 搜索好友
     */
    @GetMapping("/search")
    public Mono<Result<List<UserResponse>>> searchFriends(@RequestParam String keyword) {
        return friendService.searchFriends(keyword)
                .map(Result::success)
                .doOnSuccess(result -> log.debug("搜索好友: keyword={}", keyword));
    }

    /**
     * 获取好友状态
     */
    @GetMapping("/{userId}/status")
    public Mono<Result<FriendStatusResponse>> getFriendStatus(@PathVariable Long userId) {
        return friendService.getFriendStatus(userId)
                .map(status -> {
                    FriendStatusResponse response = new FriendStatusResponse();
                    response.setStatus(status.isOnline() ? "online" : "offline");
                    response.setLastActiveTime(status.getLastActiveTime());
                    return Result.success(response);
                })
                .doOnSuccess(result -> log.debug("获取好友状态: userId={}", userId));
    }

    /**
     * 添加好友
     */
    @PostMapping
    public Mono<Result<Void>> addFriend(@RequestBody AddFriendRequest request, @RequestParam(required = false) String username) {
        final String finalUsername = username != null ? username : "defaultUser";
        return friendService.addFriend(finalUsername, request.getUserId())
                .map(success -> Result.<Void>success())
                .doOnSuccess(result -> log.info("添加好友: {} -> {}", finalUsername, request.getUserId()));
    }

    /**
     * 删除好友
     */
    @DeleteMapping("/{userId}")
    public Mono<Result<Void>> removeFriend(@PathVariable Long userId, @RequestParam(required = false) String username) {
        final String finalUsername = username != null ? username : "defaultUser";
        return friendService.removeFriend(finalUsername, userId)
                .map(success -> Result.<Void>success())
                .doOnSuccess(result -> log.info("删除好友: {} -> {}", finalUsername, userId));
    }

    // DTO classes
    public static class FriendListResponse {
        private List<UserResponse> friends;
        private Integer total;
        
        public List<UserResponse> getFriends() { return friends; }
        public void setFriends(List<UserResponse> friends) { this.friends = friends; }
        public Integer getTotal() { return total; }
        public void setTotal(Integer total) { this.total = total; }
    }

    public static class FriendStatusResponse {
        private String status;
        private LocalDateTime lastActiveTime;
        
        public String getStatus() { return status; }
        public void setStatus(String status) { this.status = status; }
        public LocalDateTime getLastActiveTime() { return lastActiveTime; }
        public void setLastActiveTime(LocalDateTime lastActiveTime) { this.lastActiveTime = lastActiveTime; }
    }

    public static class AddFriendRequest {
        private Long userId;
        
        public Long getUserId() { return userId; }
        public void setUserId(Long userId) { this.userId = userId; }
    }

    public static class FriendStatus {
        private boolean online;
        private LocalDateTime lastActiveTime;
        
        public boolean isOnline() { return online; }
        public void setOnline(boolean online) { this.online = online; }
        public LocalDateTime getLastActiveTime() { return lastActiveTime; }
        public void setLastActiveTime(LocalDateTime lastActiveTime) { this.lastActiveTime = lastActiveTime; }
    }
}